import * as THREE from 'three/build/three.module.js';

import jiaZaiZiYuan from './zuJian/jiaZaiZiYuan.js';

import wsPeiZhi from './zuJian/wsPeiZhi.js';

import AmmoPhysics from './wuLi/pengZhuang.js';

import chuShiHua from './zuJian/chuShiHua.js';

import kePoHuai from './wuLi/kePoHuai.js';

import jieChuJianCe from './wuLi/jieChuJianCe.js';

import chuLiMoXing from './zuJian/chuLiMoXing.js';

import kongZhi from './zuJian/kongZhi.js';

import tongBu from './zuJian/tongBu.js';

import ui from './zuJian/ui.js';

import {
	nodeFrame,
	huoYan
} from './zuJian/huoYan.js';

import houQiTeXiao from './zuJian/houQiTeXiao.js';

window ? window.THREE = THREE : globalThis.THREE = THREE;

qiDong()

async function qiDong() {
	
	let jiaZaiDom = document.getElementById('jia-zai');
	let progress = document.getElementsByTagName('progress')[0];

	let jinDuArr = [];

	jinDuArr.push(AmmoPhysics(), ...jiaZaiZiYuan());

	jinDuArr.forEach((pro, i) => {
		pro.then(() => {
			progress.value += 100 / jinDuArr.length;
		})
	})

	let [{
		physicsDispatcher,
		physicsAddMesh,
		physicsDeleteMesh,
		physicsSetMeshPosition,
		physicsStep,
		physicsMeshes
	}, diTu, ren, danHen, firetorch_1, qiangSheng] = await Promise.all(jinDuArr);
	
	jiaZaiDom.style.display = 'none';
	

	let {
		clock,
		scene,
		camera,
		renderer,
		listener,
		audioLoader
	} = chuShiHua();

	let {
		changJingObj,
		jueSeObj
	} = await chuLiMoXing(listener, audioLoader, diTu, ren, qiangSheng);

	let {
		teXiaoRender
	} = houQiTeXiao(renderer, scene, camera)

	let {
		ws,
		huoQuJueSeHao
	} = wsPeiZhi();

	let jueSeHao = await huoQuJueSeHao;

	if (typeof jueSeHao != 'number') return alert(jueSeHao);

	let jueSeWo = jueSeObj.get(jueSeHao);

	jueSeWo.userData.wo = true;

	jueSeWo.children[0].children[2].visible = false;//隐藏人物
	scene.add(jueSeWo);
	
	physicsAddMesh(jueSeWo, 80, 'Compound');

	let isPc = true;

	if (!navigator.userAgent.includes('Windows')) {
		isPc = false;
	}

	let {
		shuaXinUI,
		juJiJing
	} = ui(jueSeHao);
	shuaXinUI(jueSeWo.userData.xueLiang);

	//需要服务器同步的物体
	let tongBuWuTi = new tongBu(scene, physicsAddMesh, physicsDeleteMesh, physicsSetMeshPosition, jueSeHao,
		jueSeObj, physicsMeshes, shuaXinUI, ws, jueSeWo);

	let {
		yiDong
	} = kongZhi(camera, scene, renderer, jueSeWo, isPc, tongBuWuTi, shuaXinUI, juJiJing, physicsAddMesh,
		physicsSetMeshPosition,
		ws, huoYan, listener, audioLoader, firetorch_1);

	function tianJiaObj() {

		changJingObj.forEach(obj => {

			scene.add(obj);

			if (obj.name.indexOf('jing') > -1) physicsAddMesh(obj, 0, 'Compound');
			if (obj.name.indexOf('dong') > -1) physicsAddMesh(obj, 200, 'Compound');

			if (obj.name.indexOf('sui') > -1) {

				physicsAddMesh(obj, 200, 'Convex');
				kePoHuai(obj);
			}
		})
	}

	tianJiaObj();

	function yiChuWuTi(object) {
		scene.remove(object);
		physicsDeleteMesh(object);
	}

	function animate() {

		requestAnimationFrame(animate);

		render();

	}

	animate();

	function render() {

		const deltaTime = clock.getDelta();

		jueSeObj.forEach(o => {
			o.userData.mixer.update(deltaTime)
		})//动画

		nodeFrame.update(); //火焰刷新

		updatePhysics(deltaTime);

		yiDong(deltaTime);

		tongBuWuTi.shuaXin(); //更新服务端

		// renderer.render(scene, camera);
		teXiaoRender(); //发光

	}

	function updatePhysics(deltaTime) {

		physicsStep(deltaTime)
		jieChuJianCe(scene, physicsAddMesh, physicsSetMeshPosition, yiChuWuTi, physicsDispatcher, physicsMeshes,
			jueSeWo, ws, danHen)

	}
}
